package com.bzframework.pro.utils;

import com.bzframework.pro.enums.GradeEnum;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author bingzhong
 * @Date 2025/3/30 16:36
 * @Description
 */
public class DateFormatterUtils {

    private DateFormatterUtils() {
    }

    static long ONE_YEAR_MILL = 1000L * 60 * 60 * 24 * 365;

    /**
     * 日期格式转换 yyyyMMddHHmmss  => yyyy-MM-dd HH:mm:ss
     *
     * @param originalDateStr
     * @return
     */
    public static String format(String originalDateStr) {

        return LocalDateTime.parse(originalDateStr, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
                            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static int calGrade(String originalDateStr, int gradeCode) {

        if (gradeCode >= GradeEnum.highGraduate.getCode()) {
            return gradeCode;
        }

        long inputTime = LocalDateTime.parse(originalDateStr, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
                                      .atZone(ZoneId.systemDefault())
                                      .toInstant()
                                      .toEpochMilli();

        long currentTimeMillis = System.currentTimeMillis();
        long subtract = currentTimeMillis - inputTime;
        int intValue = BigDecimal.valueOf(subtract)
                          .divide(BigDecimal.valueOf(ONE_YEAR_MILL), 4, RoundingMode.DOWN)
                          .intValue();
        int targetCode = gradeCode + intValue * 10;
        if (targetCode >= GradeEnum.highGraduate.getCode()) {
            return GradeEnum.highGraduate.getCode();
        }
        return targetCode;

    }


    public static void main(String[] args) {
        int i = calGrade("20221023133259", 150);
        System.out.println("i = " + i);
    }

}
